package com.zsqat.eshop.product.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.zsqat.eshop.product.entity.AttrEntity;
import com.zsqat.eshop.product.service.AttrService;
import com.zsqat.eshop.product.vo.AttrGroupWithAttrsVo;
import com.zsqat.eshop.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.Query;
import com.zsqat.eshop.product.dao.AttrGroupDao;
import com.zsqat.eshop.product.entity.AttrGroupEntity;
import com.zsqat.eshop.product.service.AttrGroupService;

import javax.annotation.Resource;

@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
  @Resource
  private AttrService attrService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<AttrGroupEntity> page = this.page(
        new Query<AttrGroupEntity>().getPage(params),
        new QueryWrapper<>()
    );

    return new PageUtils(page);
  }

  @Override
  public PageUtils queryPage(Map<String, Object> params, long categoryId) {
    // 前台传过来的查询关键字
    // 这个关键字是模糊查询，可能是分组Id，组名，描述
    // SELECT * FROM pms_attr_group WHERE category_id=? AND(attr_group_id=key OR attr_group_name LIKE %key%)
    String key = (String) params.get("key");
    QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
    if (!StringUtils.isEmpty(key)) {
      wrapper.and((x) -> {
        x.eq("attr_group_id", key)
            .or()
            .like("attr_group_name", key); // like: %xxx%, likeLift:%xxx, likeRight: xxx%
      });
    }

    // 如果categoryId=0,查询所有
    if (categoryId != 0) {
      wrapper.eq("category_id", categoryId);
    }
    IPage<AttrGroupEntity> page = this.page(
        new Query<AttrGroupEntity>().getPage(params),
        wrapper
    );
    return new PageUtils(page);
  }

  /**
   * 根据分类id查询出所有的分组以及这些组里面的属性
   *
   * @param categoryId
   * @return
   */
  @Override
  public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCategoryId(Long categoryId) {
    // 1.查询分组信息
    List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id", categoryId));
    // 2.查询所有属性
    return attrGroupEntities.stream().map(x -> {
      AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
      BeanUtils.copyProperties(x, attrGroupWithAttrsVo);
      List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
      attrGroupWithAttrsVo.setAttrs(attrs);
      return attrGroupWithAttrsVo;
    }).collect(Collectors.toList());
  }

  @Override
  public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long categoryId) {

    //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
    AttrGroupDao baseMapper = this.getBaseMapper();
    List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId, categoryId);

    return vos;
  }
}